﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static MrPP.SerriedCabinet.CabinetGroup;

using System.Linq;
using System;

namespace MrPP.SerriedCabinet
{
    public class CellCheckManager : GDGeek.Singleton<CellCheckManager>
    {
        public class Data {
            public Dictionary<string, ItemData> datas = new Dictionary<string, ItemData>();
            public HashSet<string> set = new HashSet<string>();
            public int i = 0;
            public string check;
        }

        private Dictionary<BoxLocation, Data> map_ = new Dictionary<BoxLocation, Data>();
        [SerializeField]
        private BoxManager _manager;




        CabinetGroup.ItemData[] datas_ = null;
        internal void setup(CabinetGroup.ItemData[] datas)
        {
            datas_ = datas;
            refresh();
          
        }


        List<string> list_ = null;
        internal void check(List<string> list)
        {
            
            list_ = list;
            refresh();
        }
        private bool open_ = false;
        public void open() {
            open_ = true;
            draw();
        }
        public void close() {
            clear();
            open_ = false;
        }
        private void draw()
        {
            foreach (var kv in map_)
            {
                Cell cell = _manager.getCell(kv.Key);
                if (cell != null) { 
                    CellDraw cd = cell.gameObject.GetComponent<CellDraw>();
                    cd.show(true, kv.Key.type);
                    int left = kv.Value.datas.Count - kv.Value.set.Count;
                    int all = kv.Value.datas.Count;
                    cd.setCheck(all.ToString(), left.ToString(), kv.Value.check, cell.location.location, ((float)(kv.Value.set.Count) / (float)(all)));
                    cd.check._goods.text = "";
                    foreach (var item in kv.Value.datas) {
                        if (!kv.Value.set.Contains(item.Key)) {
                            cd.check._goods.text = item.Value.text;
                            break;
                        }
                           
                    }
                }

            }
            //next();
        }


        private void clear()
        {
            foreach (var kv in map_)
            {
                Cell cell = _manager.getCell(kv.Key);
                if (cell != null) { 
                    CellDraw cd = cell.gameObject.GetComponent<CellDraw>();
             
                    cd.close();
                }
            }

            map_.Clear();
        }
        private void refresh() {
            if (!open_) {
                return;
            }
            clear();
            foreach (var data in datas_)
            {
                if (!map_.ContainsKey(data.location))
                {
                    map_[data.location] = new Data();
                }
                map_[data.location].datas[data.sn] = data;
            }

            if (list_ != null) { 
                foreach (string sn in list_)
                {
                    foreach (var kv in map_)
                    {
                        if (kv.Value.datas.ContainsKey(sn))
                        {
                            kv.Value.set.Add(sn);
                            kv.Value.check = kv.Value.datas[sn].text;
                        }
                    }
                }
            }
            draw();

        }
       
    }
}